#ifndef YINGCE_H
#define YINGCE_H
#include "globaltype.h"
#include "libs/base/task/taskgroup.h"
#include <QDir>
#include <QObject>
#include <QPointer>

namespace yc {
class AutoUpdateDialog;
class Downloader;
class LocalUser;
class Model;
}

class BridgeObjectManager;
class KeepTokenLive;
class YingCeWindow;

class YingCe : public QObject
{
    Q_OBJECT
public:
    explicit YingCe(QObject* parent = nullptr);
    ~YingCe();

    static YingCe* instance();

    BridgeObjectManager* bridgeObjectManager() const { return bridge_object_manager_; }
    yc::TaskGroup* mainTaskQueue() const { return main_task_queue_; }
    yc::TaskGroup* authTaskQueue() const { return auth_task_queue_; }
    yc::LocalUser* localUser() const { return local_user_; }
    yc::Model* model() const { return model_; }
    yc::Downloader* downloader() const { return downloader_; }

    void showMainWindow();

    // user
    bool login();
    void logout();
    void registerUser();
    void editUserProfile();
    void changePassword();
    void deleteUser();
    void requestStop(ResolveCallback resolve, RejectCallback reject);
    void quitApp(bool forced = false);
    void downloadAvatar(const QString& url);

    // 自动更新
    void autoCheckUpgrade();
    // 检查更新
    void checkUpgrade();
    void startAutoUpdate(const QString& latest_version);

private:
    void onAutoCheckUpgradeFinished(bool new_version, bool necessary, const QString& major_version,
                                    const QString& full_version, const QString& date);
    void onManualCheckUpgradeFinished(bool new_version, bool necessary,
                                      const QString& major_version, const QString& full_version,
                                      const QString& date);
    void checkLatestVersion();
    void showUpgradeDialog(bool new_version, const QString& major_version,
                           const QString& full_version, const QString& date);

    static YingCe* instance_;
    BridgeObjectManager* bridge_object_manager_;
    yc::TaskGroup* main_task_queue_;
    yc::TaskGroup* auth_task_queue_;
    yc::LocalUser* local_user_;
    yc::Model* model_;
    yc::Downloader* downloader_;
    QPointer<yc::AutoUpdateDialog> auto_update_dialog_;
    bool installing_pack_ = false;
    KeepTokenLive* keep_token_live_;
    QPointer<YingCeWindow> window_;
};

#endif // YINGCE_H
